package com.tencent.wuyanzu.whattoeat.net;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.tencent.wuyanzu.whattoeat.Global.ARGS;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by bleuesprit on 2015-10-03.
 * ok
 */
public class NetUtil {

    static private HttpClient client = new DefaultHttpClient();
    ;
    static String URLPREV;
    static private HashMap<String, String> netRequestHeads = new HashMap<String, String>();

    static {
        URLPREV = ARGS.HOSTPRE;      /* http:\\localhost:8080\ */


    }

    public static void postServer(final String url, final JSONObject message, final Handler resultHandler) {

        postServer(url, message, resultHandler, 0x123);
    }


    public static void postServer(final String url, final JSONObject message, final Handler resultHandler, final int messageWhat) {

        final int i = 0;
        new Thread() {
            @Override
            public void run() {
                Log.i("NetUtil", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + (i));
                String response = null;

                //使用apache HTTP客户端实现
                String urlStr = URLPREV + url;
                HttpPost request = new HttpPost(urlStr);

                //如果传递参数多的话，可以对传递的参数进行封装
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                //添加参数
                params.add(new BasicNameValuePair("jsonString", message.toString()));

                try {
                    //设置请求参数项
                    request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));


                    //执行请求返回相应
                    HttpResponse httpResponse = client.execute(request);


                    //判断是否请求成功
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        //获得响应信息
                        response = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                    } else {
                        switch (httpResponse.getStatusLine().getStatusCode()) {
                            case 404:
                                Log.e("NetUtil", "http result code is not 404");
                                break;
                            default:
                                Log.e("NetUtil", "http result code is not 200");
                                break;
                        }

                    }

                } catch (Exception e) {
                    Log.e("NetUtil", "http get connection error");
                    e.printStackTrace();
                }
                if (response == null) {
                    return;
                }

                Message resultMessage = new Message();
                resultMessage.what = messageWhat;
                Bundle bundle = new Bundle();
                bundle.putString("result", response);
                resultMessage.setData(bundle);
                resultHandler.sendMessage(resultMessage);
            }
        }.start();
    }


    public static void executeGet(final String url, final  Handler resultHandler,final int messageWhat)
    {

        new Thread() {
            @Override
            public void run() {
                String response = null;
                try {
                    // 实例化HTTP方法
                    HttpGet request = new HttpGet();
                    request.setURI(new URI(URLPREV+url));
                    Log.i("net",new URI(URLPREV+url).toString());
                    HttpResponse httpResponse = client.execute(request);
                    //判断是否请求成功
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        //获得响应信息
                        response = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                    } else {
                        switch (httpResponse.getStatusLine().getStatusCode()) {
                            case 404:
                                Log.e("NetUtil", "http result code is not 404");
                                break;
                            default:
                                Log.e("NetUtil", "http result code is not 200");
                                break;
                        }

                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

                Message resultMessage = new Message();
                resultMessage.what = messageWhat;
                Bundle bundle = new Bundle();
                Log.i("net",response);
                bundle.putString("result", response);
                resultMessage.setData(bundle);
                resultHandler.sendMessage(resultMessage);
            }
        }.start();
    }

    public static void getPic(final String picURL, final  Handler resultHandler,final int messageWhat,final Bitmap bitmap[])
    {
        new Thread()
        {
            public void run() {


                HttpGet httpget = new HttpGet(URLPREV+picURL);

                try {
                    HttpResponse resp = client.execute(httpget);
                    //判断是否正确执行
                    if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
                        //将返回内容转换为bitmap
                        HttpEntity entity = resp.getEntity();
                        InputStream in = entity.getContent();

                        bitmap[0] = BitmapFactory.decodeStream(in);

                        //向handler发送消息，执行显示图片操作
                        Message msg = new Message();
                        msg.what = messageWhat;
                        resultHandler.sendMessage(msg);
                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        } .start();
    }
}



